package com.project.service.activityuser;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.project.dao.activityuser.ActivityUserDao;
import com.project.entity.activity.ActivityUser;
import com.project.entity.sms.SmsMessageVo;
import com.project.service.BaseService;
import com.project.service.sms.SmsService;
import com.project.service.sys.AreaService;
import com.project.util.ExcelUtil;

@Service
public class ActivityUserService extends BaseService<ActivityUser>{
	
private static final Logger logger = LoggerFactory.getLogger(ActivityUserService.class);
	
	@Autowired
	private ActivityUserDao activityUserDao;
	@Autowired
	private AreaService areaService;
	@Autowired
	private SmsService smsService;
	
	public void sendSmsForActivityUserReminder() {
		List<Map<String,Object>> list = activityUserDao.getActivityUserListByTomorrowBegin();
		if(list!=null && !list.isEmpty()){
			for (Map<String, Object> map : list) {
				logger.info("sendSmsForActivityUserReminder every object : {}",map);
				String username = (String) map.get("username");
				String mobile = (String) map.get("mobile");
				String activityName = (String) map.get("activityName");
				String activityStartDate = (String) map.get("activityStartDate");
				String province = (String) map.get("province");
				String city = (String) map.get("city");
				String district = (String) map.get("district");
				String provinceName = "";
				String cityName = "";
				String districtName = "";
				if(StringUtils.isNotBlank(province))
					provinceName = areaService.getAreaNameByCode(province);
				if(StringUtils.isNotBlank(city))
					cityName = areaService.getAreaNameByCode(city);
				if(StringUtils.isNotBlank(district))
					districtName = areaService.getAreaNameByCode(district);
				String address = provinceName+cityName+districtName+(String) map.get("address");
				//尊敬的{1}先生/女士，您参加的“{3}”活动即将开始，请准时参加活动。活动时间：{4}，活动地点：{6}。
				SmsMessageVo smsMessageVo = new SmsMessageVo();
				smsMessageVo.setTaget(mobile);
				smsMessageVo.setSendPoint("SMS_SEND_ACTIVITY_USER_REMINDER");
				smsMessageVo.addValue("username", username);
				smsMessageVo.addValue("servicename", activityName);
				smsMessageVo.addValue("servicetime", activityStartDate);
				smsMessageVo.addValue("reason", address);
				smsService.sendSms(smsMessageVo);
			}
		}
		
	}

	public void downloadExcel(String fileName,List<Map<String, Object>> list,HttpServletResponse response){
		String []keys = new String[]{"userCode","name","position","addr","auditStatus","auditDescription","cancelReason"};
		String columnNames[] = new String[]{"账号","姓名","职位","地址","审批状态","说明","备注"};
		try {
			
			ExcelUtil.exportExcel(fileName, list, keys, columnNames, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}
